﻿//this should be included from Site.Master

var DEFAULT_DLG_WIDTH = 350;
var DEFAULT_DLG_HEIGHT = 500;

var SEARCH_PAGE_URL = "";
var CANDIDATE_PAGE_URL = "";

var AllowSearch = true;

//this function runs on document.ready()
$(function () {
    SetupNavBar();

    //let all inputs be able to be watermarked by 'Title'
    $('.WatermarkInput').focus(function () {
        if ($(this).val() == $(this).attr("title")) {
            $(this).val("");
            $(this).removeClass("WatermarkInput");
        }
    }).blur(function () {
        if ($(this).val() == "") {
            $(this).val($(this).attr("title"));
            $(this).addClass("WatermarkInput");
        }
    }).each(function (i) {
        $(this).val($(this).attr("title"));
    });
});

function SetupNavBar() {
    //setup our nav bar

    $("#NavBarBtnNewCand").button({
        icons: {
            primary: "ui-icon-plusthick"
        }
    });
    $("#NavBarBtnHome").button({});
    $("#NavBarBtnDashboard").button({
        icons: {
            primary: "ui-icon-calculator"
        }
    });
    $("#NavBarBtnCandidates").button({
        icons: {
            primary: "ui-icon-person"
        }
    });
    $("#NavBarBtnEvents").button({
        icons: {
            primary: "ui-icon-calendar"
        }
    });


    $("#SettingsBtn").button({
        icons: {
            primary: "ui-icon-gear"
        }
    });

    //hide dropdown menu when menu loses focus
    //******TODO: this DOES NOT WORK in Chrome or Firefox!!! ***********************
    //for now, we will not do this, since it needs to work in all browsers or none
    //$("#AddItemMenu").blur(function () { OnAddNewMenuLoseFocus(); });

    //setup the search autocomplete
    $("#NavSearch").autocomplete({
        source: CandidateSearchSource,
        select: function (event, ui) {
            //candidate selected, go to candidate view
            AllowSearch = false;
            window.location = CANDIDATE_PAGE_URL + '?candID=' + ui.item.itemID;
            return false;
        },
        delay: 10 //todo how does this feel with lots of data?
    });

    //catch the 'enter' keydown for the 'search' control
    //route to candidate list view
    $("#NavSearch").keydown(function (e) {
        if ((e.keyCode == 13) && (AllowSearch)) {
            window.location = SEARCH_PAGE_URL + '?q=' + $(this).val();
            //keep weird behavior from occuring
            e.preventDefault();
        }
    });
}

function CandidateSearchSource(request, response) {
    //retrieve candidates based on search value in input
    PageMethods.GetAllCandidates_Json(request.term,
    function (json) {
        //send the recieved data to the control
        response(eval(json));

    },
    function () {
        //error occured, set empty data
        response([]);
    }
    );
}

function SwitchToView(url) {
    window.location = url;
    return false;
}

function ShowPopup(strPopup) {
    $(strPopup).dialog("open");
    return false;
}
